package com.samuel.learning.multithread.interview;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * How to hold the ordering of multiThread
 */
public class ThreadOrding {

    public static Thread  thread1 = new Thread(()->{
        System.out.println("thread1");
    });
    public static Thread  thread2 = new Thread(()->{
        System.out.println("thread2");
    });
    public static  Thread  thread3 = new Thread(()->{
        System.out.println("thread3");
    });


    public static void main(String[] args) {

        /*try {

            // 1. join() 保证 主线程在子线程 在执行完后，才开始主线程
            // join() Object 的 wait();
            thread1.start();
            thread1.join();
            thread2.start();
            thread2.join();
            thread3.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        // 向SingleThreadExecutor提交多个任务，这些任务将排队。从输出结果可以看到，任务按照提交顺序被执行
        //FIFO
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(thread1);
        executorService.submit(thread2);
        executorService.submit(thread3);
        executorService.shutdown();
    }

}
